// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成

package com.fowo.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fowo.api.common.annotaion.DataObjectType;
import com.fowo.api.common.config.CacheConfig;
import com.fowo.api.common.excel.CustomExcelExport;
import com.fowo.api.common.excel.CustomExcelHandler;
import com.fowo.api.common.excel.ExcelConfig;
import com.fowo.api.common.excel.TemplateModelBuildEventListener;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.model.JoinModel;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.RException;
import com.fowo.api.common.util.DateUtils;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.common.util.SheetUtils;
import com.fowo.api.entity.SalesOutbound;
import com.fowo.api.flow.entity.WorkFlow;
import com.fowo.api.flow.service.WorkFlowService;
import com.fowo.api.mapper.SalesOutboundMapper;
import com.fowo.api.mapper.ShopInfoMapper;
import com.fowo.api.mapper.StockOutMapper;
import com.fowo.api.model.sales.outbound.SalesOutboundExcelPo;
import com.fowo.api.model.sales.outbound.SalesOutboundImportPo;
import com.fowo.api.model.sales.outbound.SalesOutboundItemVo;
import com.fowo.api.model.sales.outbound.SalesOutboundSearchParamPo;
import com.fowo.api.model.sales.outbound.SalesOutboundShopPushAction;
import com.fowo.api.model.sales.outbound.SalesOutboundVo;
import com.fowo.api.model.sales.outbound.constant.SalesOutboundLogisticsProviderEnums;
import com.fowo.api.model.shop.info.ShopInfoItemVo;
import com.fowo.api.model.shop.info.ShopInfoSearchParamPo;
import com.fowo.api.model.stock.out.StockOutItemVo;
import com.fowo.api.model.stock.out.StockOutSearchParamPo;
import com.fowo.api.service.SalesOutboundService;
import com.fowo.api.sys.annotation.DataActivity;
import com.fowo.api.sys.component.SysDataActivityAopHelper;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.sys.entity.SysImportTemplate;
import com.fowo.api.sys.entity.enums.SysDataActivityAction;
import com.fowo.api.sys.mapper.SysDepartmentMapper;
import com.fowo.api.sys.model.NewSerialRequest;
import com.fowo.api.sys.model.SysDepartmentSearchParamPo;
import com.fowo.api.sys.model.SysDepartmentVo;
import com.fowo.api.sys.model.SysDictAllItem;
import com.fowo.api.sys.service.FileService;
import com.fowo.api.sys.service.SysDictService;
import com.fowo.api.sys.service.SysImportTemplateService;
import com.fowo.api.sys.service.SysSerialService;
import com.fowo.api.user.model.JwtUserInfo;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * 销售出库单 服务实现类
 * @author 蜂窝蜜造平台 配置人：鹿丙涛，吕家傲
 */
@Service
@Slf4j
@DataObjectType(SalesOutbound.class)
@DS("ds24")
public class SalesOutboundServiceImpl
  extends ServiceImpl<SalesOutboundMapper, SalesOutbound>
  implements SalesOutboundService {

  /** 引用字段排序字段名转换信息 */
  public static final Map<String, String> SORT_FIELD_MAP = new HashMap<>() {
    {
      put("shopShopName", "si.`shop_name`");
      put("shippingClerkOrderSn", "so.`order_sn`");
      put("deptName", "sd.`name`");
    }
  };
  // 导出时单次查询最大记录数
  protected static final int EXPORT_QUERY_MAX_SIZE = 1000;
  // 支持导出的最大记录数(1048576 为 Excel 支持的最大行数)
  protected static final long EXPORT_MAX_SIZE = 1048575;

  /**
   * 服务自引用，用于调用服务方法时包括 AOP 处理
   */
  @Resource
  protected SalesOutboundService self;

  @Resource
  protected SysSerialService sysSerialService;

  @Resource
  private SysDictService sysDictService;

  @Resource
  protected ShopInfoMapper shopInfoMapper;

  @Resource
  protected StockOutMapper stockOutMapper;

  @Resource
  protected SysDepartmentMapper sysDepartmentMapper;

  @Resource
  protected FileService fileService;

  @Resource
  protected SysImportTemplateService importTemplateService;

  @Resource
  private CustomExcelExport customExcelExport;

  @Resource
  protected WorkFlowService workFlowService;

  @Autowired(required = false)
  protected SalesOutboundShopPushAction salesOutboundShopPushAction;

  @Resource
  protected Validator validator;

  /**
   * 创建销售出库单
   * @param model 销售出库单
   * @return 新数据的主键
   */
  @DataActivity(action = SysDataActivityAction.Add)
  @Transactional(rollbackFor = Exception.class)
  @Override
  public Long create(SalesOutboundVo model) throws Exception {
    JwtUserInfo currentUser = JwtUserInfo.fromHeader();
    SalesOutbound entity = new SalesOutbound();
    BeanUtils.copyProperties(model, entity);
    entity.setCreateTime(new Date());
    entity.setLastUpdateTime(new Date());
    if (currentUser != null) {
      entity.setCreateUser(currentUser.getUserId());
      entity.setLastUpdateUser(currentUser.getUserId());
    }
    // 填充序列号
    if (!StringUtils.hasText(entity.getSalesReleaseNumber())) {
      entity.setSalesReleaseNumber(
        sysSerialService.getNewSerial(
          new NewSerialRequest(null, "SalesOutbound.salesReleaseNumber")
        )
      );
    }
    // 填充序列号
    if (!StringUtils.hasText(entity.getSecondHarmonicSignal())) {
      entity.setSecondHarmonicSignal(
        sysSerialService.getNewSerial(
          new NewSerialRequest(null, "SalesOutbound.secondHarmonicSignal")
        )
      );
    }
    // 填充序列号
    if (!StringUtils.hasText(entity.getPlatformNumber())) {
      entity.setPlatformNumber(
        sysSerialService.getNewSerial(
          new NewSerialRequest(null, "SalesOutbound.platformNumber")
        )
      );
    }
    if (this.save(entity)) {
      return entity.getId();
    }
    throw new Exception("销售出库单保存失败");
  }

  /**
   * 更新销售出库单
   * @param model 销售出库单
   * @return 更新是否成功
   */
  @DataActivity(action = SysDataActivityAction.Modified)
  @Override
  @Transactional(rollbackFor = Exception.class)
  public boolean update(SalesOutboundVo model) throws Exception {
    JwtUserInfo currentUser = JwtUserInfo.fromHeader();
    SalesOutbound entity = this.getById(model.getId());
    if (entity == null) {
      throw new Exception("销售出库单不存在");
    }
    BeanUtils.copyProperties(
      model,
      entity,
      "id",
      "createTime",
      "createUser",
      "lastUpdateTime",
      "lastUpdateUser",
      "settlementId"
    );
    entity.setLastUpdateTime(new Date());
    if (currentUser != null) {
      entity.setLastUpdateUser(currentUser.getUserId());
    }
    if (this.updateById(entity)) {
      return true;
    }
    return false;
  }/**
   * 更新销售出库单（带空值）
   * @param model 销售出库单
   * @return 更新是否成功
   */

  @DataActivity(action = SysDataActivityAction.Modified)
  @Override
  @Transactional(rollbackFor = Exception.class)
  public boolean updateForEdit(SalesOutboundVo model) throws Exception {
    JwtUserInfo currentUser = JwtUserInfo.fromHeader();
    SalesOutbound entity = this.getById(model.getId());
    if (entity == null) {
      throw new Exception("销售出库单不存在");
    }
    model.setLastUpdateTime(new Date());
    if (currentUser != null) {
      model.setLastUpdateUser(currentUser.getUserId());
    }
    boolean isOk = new LambdaUpdateChainWrapper<>(baseMapper)
      .set(SalesOutbound::getLastUpdateTime, model.getLastUpdateTime())
      .set(SalesOutbound::getLastUpdateUser, model.getLastUpdateUser())
      .set(SalesOutbound::getSalesReleaseNumber, model.getSalesReleaseNumber())
      .set(SalesOutbound::getSystemMenuSymbol, model.getSystemMenuSymbol())
      .set(SalesOutbound::getStatus, model.getStatus())
      .set(
        SalesOutbound::getSecondHarmonicSignal,
        model.getSecondHarmonicSignal()
      )
      .set(SalesOutbound::getPlatformNumber, model.getPlatformNumber())
      .set(SalesOutbound::getLogisticsProvider, model.getLogisticsProvider())
      .set(SalesOutbound::getTransport, model.getTransport())
      .set(SalesOutbound::getTally, model.getTally())
      .set(SalesOutbound::getFid, model.getFid())
      .set(SalesOutbound::getAmazonOrderId, model.getAmazonOrderId())
      .set(SalesOutbound::getFulfillment, model.getFulfillment())
      .set(SalesOutbound::getCountryCode, model.getCountryCode())
      .set(SalesOutbound::getAccountType, model.getAccountType())
      .set(SalesOutbound::getEventType, model.getEventType())
      .set(SalesOutbound::getSellerSku, model.getSellerSku())
      .set(SalesOutbound::getLocalSku, model.getLocalSku())
      .set(SalesOutbound::getLocalName, model.getLocalName())
      .set(SalesOutbound::getType, model.getType())
      .set(SalesOutbound::getPostedDateLocale, model.getPostedDateLocale())
      .set(SalesOutbound::getCurrencyCode, model.getCurrencyCode())
      .set(SalesOutbound::getCurrencyAmount, model.getCurrencyAmount())
      .set(SalesOutbound::getQuantity, model.getQuantity())
      .set(SalesOutbound::getProcessingStatus, model.getProcessingStatus())
      .set(SalesOutbound::getFundTransferStatus, model.getFundTransferStatus())
      .set(SalesOutbound::getGmtModified, model.getGmtModified())
      .set(SalesOutbound::getDept, model.getDept())
      .set(
        SalesOutbound::getCurrentIssueCostAmount,
        model.getCurrentIssueCostAmount()
      )
      .set(SalesOutbound::getProductId, model.getProductId())
      .set(SalesOutbound::getSid, model.getSid())
      .set(SalesOutbound::getSellerName, model.getSellerName())
      .eq(SalesOutbound::getId, model.getId())
      .update();
    if (isOk) {}
    return isOk;
  }

  /**
   * 是否走导出中心
   */
  @Override
  public boolean getBackgroundExportFlag(SalesOutboundSearchParamPo search) {
    return false;
  }

  /**
   * 删除销售出库单
   * @param id 销售出库单的主键
   * @return 删除是否成功
   */
  @DataActivity(action = SysDataActivityAction.Del)
  @Override
  public boolean delete(Long id) throws Exception {
    return this.removeById(id);
  }

  /**
   * 批量删除销售出库单
   * @param ids 销售出库单的主键列表
   * @return 删除是否成功
   */
  @DataActivity(action = SysDataActivityAction.Del)
  @Override
  public boolean batchDelete(List<Long> ids) throws Exception {
    int count = 0;
    for (int i = 0; i < ids.size(); i += 2000) {
      count +=
        baseMapper.deleteBatchIds(
          ids.stream().skip(i).limit(2000).collect(Collectors.toList())
        );
    }
    return count > 0;
  }

  /**
   * 导入单行处理
   * @param row 要导入的行
   * @param rowNumber 所在行号
   * @param vo 输出VO,可空
   * @param variables 变量池，用于在一次导入过程中保存临时信息
   * @param allowOverrides 允许覆盖
   * @return 导入行对象
   */
  protected ImportRow<SalesOutboundImportPo> excelToImportRow(
    SalesOutboundImportPo row,
    Integer rowNumber,
    SalesOutboundVo vo,
    Map<String, Object> variables,
    boolean allowOverrides
  ) {
    ImportRow<SalesOutboundImportPo> ir =
      new ImportRow<SalesOutboundImportPo>();
    ir.setRowNumber(rowNumber);
    ir.setRow(row);
    if (SheetUtils.isAllFieldsEmpty(row)) {
      ir.setEmptyRow(true);
      ir.addError("", "空行");
      return ir;
    }
    final Set<ConstraintViolation<SalesOutboundImportPo>> validate =
      validator.validate(row);
    boolean isOutputVo = vo != null;
    if (!isOutputVo) {
      vo = new SalesOutboundVo();
    }
    BeanUtils.copyProperties(row, vo);
    // 收集基本校验错误
    ir.initErrors(validate, SalesOutboundImportPo.class);

    // 一般数据处理
    if (StringUtils.hasText(row.getStatus())) {
      List<SysDictAllItem> statusItems =
        (List<SysDictAllItem>) variables.computeIfAbsent(
          "status",
          n -> sysDictService.getDictItems("SCM00150")
        );
      SalesOutboundVo finalVo = vo;
      statusItems
        .stream()
        .filter(i -> i.getItemText().equals(row.getStatus()))
        .findFirst()
        .ifPresentOrElse(
          i -> finalVo.setStatus(i.getItemValue()),
          () ->
            ir.addError(
              "status",
              String.format(
                "必须为 %s 中之一",
                statusItems
                  .stream()
                  .map(SysDictAllItem::getItemText)
                  .collect(Collectors.joining(", "))
              )
            )
        );
    }
    if (StringUtils.hasText(row.getLogisticsProvider())) {
      vo.setLogisticsProvider(
        SalesOutboundLogisticsProviderEnums.toValue(row.getLogisticsProvider())
      );
      if (vo.getLogisticsProvider() == null) {
        ir.addError("logisticsProvider", "物流商只能是：“EBO”, “速卖通”");
      }
    }
    vo.setDeliveryTime(SheetUtils.tryToDateTime(row.getDeliveryTime()));
    vo.setPostedDateLocale(SheetUtils.tryToDateTime(row.getPostedDateLocale()));
    if (StringUtils.hasText(row.getCurrencyCode())) {
      List<SysDictAllItem> currencyCodeItems =
        (List<SysDictAllItem>) variables.computeIfAbsent(
          "currencyCode",
          n -> sysDictService.getDictItems("SCM00015")
        );
      SalesOutboundVo finalVo = vo;
      currencyCodeItems
        .stream()
        .filter(i -> i.getItemText().equals(row.getCurrencyCode()))
        .findFirst()
        .ifPresentOrElse(
          i -> finalVo.setCurrencyCode(i.getItemValue()),
          () ->
            ir.addError(
              "currencyCode",
              String.format(
                "必须为 %s 中之一",
                currencyCodeItems
                  .stream()
                  .map(SysDictAllItem::getItemText)
                  .collect(Collectors.joining(", "))
              )
            )
        );
    }
    vo.setCurrencyAmount(SheetUtils.tryToDecimal(row.getCurrencyAmount()));
    vo.setQuantity(SheetUtils.tryToInteger(row.getQuantity()));
    if (StringUtils.hasText(row.getProcessingStatus())) {
      List<SysDictAllItem> processingStatusItems =
        (List<SysDictAllItem>) variables.computeIfAbsent(
          "processingStatus",
          n -> sysDictService.getDictItems("SCM00027")
        );
      SalesOutboundVo finalVo = vo;
      processingStatusItems
        .stream()
        .filter(i -> i.getItemText().equals(row.getProcessingStatus()))
        .findFirst()
        .ifPresentOrElse(
          i -> finalVo.setProcessingStatus(i.getItemValue()),
          () ->
            ir.addError(
              "processingStatus",
              String.format(
                "必须为 %s 中之一",
                processingStatusItems
                  .stream()
                  .map(SysDictAllItem::getItemText)
                  .collect(Collectors.joining(", "))
              )
            )
        );
    }
    if (StringUtils.hasText(row.getFundTransferStatus())) {
      List<SysDictAllItem> fundTransferStatusItems =
        (List<SysDictAllItem>) variables.computeIfAbsent(
          "fundTransferStatus",
          n -> sysDictService.getDictItems("SCM00026")
        );
      SalesOutboundVo finalVo = vo;
      fundTransferStatusItems
        .stream()
        .filter(i -> i.getItemText().equals(row.getFundTransferStatus()))
        .findFirst()
        .ifPresentOrElse(
          i -> finalVo.setFundTransferStatus(i.getItemValue()),
          () ->
            ir.addError(
              "fundTransferStatus",
              String.format(
                "必须为 %s 中之一",
                fundTransferStatusItems
                  .stream()
                  .map(SysDictAllItem::getItemText)
                  .collect(Collectors.joining(", "))
              )
            )
        );
    }
    vo.setGmtModified(SheetUtils.tryToDateTime(row.getGmtModified()));
    vo.setCurrentIssueCostAmount(
      SheetUtils.tryToDecimal(row.getCurrentIssueCostAmount())
    );
    vo.setProductId(SheetUtils.tryToLong(row.getProductId()));
    vo.setSid(SheetUtils.tryToLong(row.getSid()));
    // 处理关联字段“店铺”
    if (
      !SheetUtils.isBlank(row.getShopShopName()) &&
      SheetUtils.isBlank(row.getShop())
    ) {
      ShopInfoSearchParamPo shopSearchParam = new ShopInfoSearchParamPo();
      shopSearchParam.setLimit(2); // 最大只返回二行记录
      shopSearchParam.setShopNameEq(SheetUtils.trim(row.getShopShopName()));
      shopSearchParam.applySqlSegments();
      List<ShopInfoItemVo> shopMatches = shopInfoMapper.search(shopSearchParam);
      if (shopMatches.size() == 0) {
        ir.addError("shopShopName", "未能在店铺中找到匹配的数据");
      } else if (shopMatches.size() > 1) {
        ir.addError("shopShopName", "关联不唯一，在店铺中找到多个匹配的数据");
      } else {
        vo.setShop(shopMatches.get(0).getId());
      }
    }
    // 处理关联字段“发货员”
    if (
      !SheetUtils.isBlank(row.getShippingClerkOrderSn()) &&
      SheetUtils.isBlank(row.getShippingClerk())
    ) {
      StockOutSearchParamPo shippingClerkSearchParam =
        new StockOutSearchParamPo();
      shippingClerkSearchParam.setLimit(2); // 最大只返回二行记录
      shippingClerkSearchParam.setOrderSnEq(
        SheetUtils.trim(row.getShippingClerkOrderSn())
      );
      shippingClerkSearchParam.applySqlSegments();
      List<StockOutItemVo> shippingClerkMatches = stockOutMapper.search(
        shippingClerkSearchParam
      );
      if (shippingClerkMatches.size() == 0) {
        ir.addError("shippingClerkOrderSn", "未能在出库单中找到匹配的数据");
      } else if (shippingClerkMatches.size() > 1) {
        ir.addError(
          "shippingClerkOrderSn",
          "关联不唯一，在出库单中找到多个匹配的数据"
        );
      } else {
        vo.setShippingClerk(shippingClerkMatches.get(0).getId());
        //20230706 调整
        final Map apiParams = MapUtil
          .builder()
          .put("id", shippingClerkMatches.get(0).getId())
          .put("fromObjectName", "SalesOutbound")
          .put("fromObjectId", vo.getId())
          .put("fromField", "shippingClerk")
          .build();

        final List<JoinModel> joinModels = new ArrayList<>(2);
        if (
          variables.get("IMPORT_FIELDS") == null ||
          !((Set<String>) variables.get("IMPORT_FIELDS")).contains(
              "deliveryTime"
            )
        ) {
          joinModels.add(
            JoinModel
              .builder()
              .sourceFieldName("outTime")
              .sourceType("field")
              .sourceApi(null)
              .targetFieldName("deliveryTime")
              .rowFieldName("deliveryTime")
              .apiParams(apiParams)
              .required(false)
              .importIgnore(false)
              .build()
          );
        }
        if (
          variables.get("IMPORT_FIELDS") == null ||
          !((Set<String>) variables.get("IMPORT_FIELDS")).contains("bourn")
        ) {
          joinModels.add(
            JoinModel
              .builder()
              .sourceFieldName("toWarehouse")
              .sourceType("field")
              .sourceApi(null)
              .targetFieldName("bourn")
              .rowFieldName("bourn")
              .apiParams(apiParams)
              .required(false)
              .importIgnore(false)
              .build()
          );
        }
        SheetUtils.joinHandle(
          null,
          ir,
          null,
          vo,
          CollUtil.getFirst(shippingClerkMatches),
          joinModels
        );
      }
    }
    // 处理关联字段“组织”
    if (
      !SheetUtils.isBlank(row.getDeptName()) &&
      SheetUtils.isBlank(row.getDept())
    ) {
      SysDepartmentSearchParamPo deptSearchParam =
        new SysDepartmentSearchParamPo();
      deptSearchParam.setLimit(2); // 最大只返回二行记录
      deptSearchParam.setNameEq(SheetUtils.trim(row.getDeptName()));
      deptSearchParam.applySqlSegments();
      List<SysDepartmentVo> deptMatches = sysDepartmentMapper.search(
        deptSearchParam
      );
      if (deptMatches.size() == 0) {
        ir.addError("deptName", "未能在部门中找到匹配的数据");
      } else if (deptMatches.size() > 1) {
        ir.addError("deptName", "关联不唯一，在部门中找到多个匹配的数据");
      } else {
        vo.setDept(deptMatches.get(0).getId());
      }
    }

    return ir;
  }

  /**
   * 导入预览
   * @param sysFile 已上传到系统的文件
   * @param templateId 要使用的模版编号
   * @param allowOverrides 允许覆盖
   */
  @Override
  public List<ImportRow<SalesOutboundImportPo>> importPreview(
    SysFile sysFile,
    Long templateId,
    boolean allowOverrides
  ) throws Exception {
    SysImportTemplate importTemplate = null;
    if (templateId != null && templateId > 0) {
      importTemplate = importTemplateService.getById(templateId);
      if (importTemplate == null) {
        throw new RException("指定的模版不可用！");
      }
    }
    Map<String, Object> variables = new HashMap<>();

    // 标记由模版直接导入的字段集
    if (importTemplate != null) {
      variables.put(
        "IMPORT_FIELDS",
        importTemplate
          .getSetting()
          .getMappings()
          .stream()
          .map(SysImportTemplate.Mapping::getField)
          .collect(Collectors.toSet())
      );
    }
    try (InputStream in = fileService.read(sysFile)) {
      List<ImportRow<SalesOutboundImportPo>> rows = new ArrayList<>();

      // 读取 Excel 到 rows
      EasyExcel
        .read(in)
        .useDefaultListener(false)
        .registerReadListener(
          new TemplateModelBuildEventListener<>(
            SalesOutboundImportPo.class,
            importTemplate
          ) {
            @Override
            public void invokeRow(
              SalesOutboundImportPo row,
              AnalysisContext cxt
            ) {
              SalesOutboundVo vo = new SalesOutboundVo();
              ImportRow<SalesOutboundImportPo> ir = excelToImportRow(
                row,
                cxt.readRowHolder().getRowIndex(),
                vo,
                variables,
                allowOverrides
              );
              if (ir.isEmptyRow()) {
                return;
              }
              if (allowOverrides) {
                // FIXME: 未在平台上配置此对象《作为导入唯一识别》的《逻辑分组》配置，无法处理更新模式
              }
              rows.add(ir);
            }
          }
        )
        .sheet()
        .doRead();

      return rows;
    }
  }

  /**
   * 完成导入
   * @param rows 预导入的行信息
   */
  @DataActivity(action = SysDataActivityAction.Import)
  @Transactional(rollbackFor = Exception.class)
  @Override
  public void importDone(
    List<ImportRow<SalesOutboundImportPo>> rows,
    Long templateId
  ) throws Exception {
    SysImportTemplate importTemplate = null;
    if (templateId != null && templateId > 0) {
      importTemplate = importTemplateService.getById(templateId);
      if (importTemplate == null) {
        throw new RException("指定的模版不可用！");
      }
    }
    String sysImportBatchNo = StrUtil.concat(
      true,
      DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_MS_PATTERN),
      IdUtil.nanoId(4)
    );
    List<Long> importedIdList = new ArrayList<>();
    SysDataActivityAopHelper.clear();
    final SysDataActivityAopHelper.DataActivityAopContext context =
      SysDataActivityAopHelper.getOrCreateContext();
    Map<String, Object> variables = new HashMap<>();

    // 标记由模版直接导入的字段集
    if (importTemplate != null) {
      variables.put(
        "IMPORT_FIELDS",
        importTemplate
          .getSetting()
          .getMappings()
          .stream()
          .map(SysImportTemplate.Mapping::getField)
          .collect(Collectors.toSet())
      );
    }
    for (int i = 0; i < rows.size(); i++) {
      ImportRow<SalesOutboundImportPo> row = rows.get(i);
      Long updateId = row.getUpdateId();
      SalesOutboundVo vo = new SalesOutboundVo();
      row =
        excelToImportRow(row.getRow(), row.getRowNumber(), vo, variables, true);
      if (row.getErrors() != null && row.getErrors().size() > 0) {
        throw new RException(
          String.format(
            "第%d行（Excel第%d行）数据验证错误：%s",
            i + 1,
            row.getRowNumber(),
            row.getJoinErrorMessage()
          )
        );
      }
      if (updateId != null) {
        vo.setId(updateId);
      }
      // 重建"销售出库单号"
      if (
        importTemplate != null &&
        importTemplate
          .getSetting()
          .getMappings()
          .stream()
          .anyMatch(m ->
            Boolean.TRUE.equals(m.getNeedRecreate()) &&
            "salesReleaseNumber".equals(m.getField())
          )
      ) {
        vo.setSalesReleaseNumber(
          sysSerialService.getNewSerial(
            new NewSerialRequest(null, "SalesOutbound.salesReleaseNumber")
          )
        );
      }

      // 重建"波次号"
      if (
        importTemplate != null &&
        importTemplate
          .getSetting()
          .getMappings()
          .stream()
          .anyMatch(m ->
            Boolean.TRUE.equals(m.getNeedRecreate()) &&
            "secondHarmonicSignal".equals(m.getField())
          )
      ) {
        vo.setSecondHarmonicSignal(
          sysSerialService.getNewSerial(
            new NewSerialRequest(null, "SalesOutbound.secondHarmonicSignal")
          )
        );
      }

      // 重建"平台单号"
      if (
        importTemplate != null &&
        importTemplate
          .getSetting()
          .getMappings()
          .stream()
          .anyMatch(m ->
            Boolean.TRUE.equals(m.getNeedRecreate()) &&
            "platformNumber".equals(m.getField())
          )
      ) {
        vo.setPlatformNumber(
          sysSerialService.getNewSerial(
            new NewSerialRequest(null, "SalesOutbound.platformNumber")
          )
        );
      }

      SheetUtils.setSysImportTemplateName(
        vo,
        null == importTemplate ? "默认模板" : importTemplate.getTitle()
      );
      SheetUtils.setSysImportBatchNo(vo, sysImportBatchNo);
      if (updateId == null) {
        importedIdList.add(create(vo));
      } else {
        self.update(vo);
        importedIdList.add(vo.getId());
      }
    }
    context.getIdList().addAll(importedIdList);
  }

  /**
   * 导出
   * @param templateId 使用模版（可以是 null 或 0 表示系统默认导出模版）
   * @param search 查询条件
   */
  @DataActivity(action = SysDataActivityAction.Export)
  @Override
  public void export(
    Long templateId,
    SalesOutboundSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    if (templateId != null && templateId > 0) {
      SysImportTemplate sysImportTemplate = importTemplateService.getById(
        templateId
      );
      if (sysImportTemplate == null) {
        throw new RException("所选的导出模版不可用");
      }
      CustomExcelExport.CustomExcelExportOptions exportOptions =
        new CustomExcelExport.CustomExcelExportOptions();
      exportOptions.setImportTemplate(sysImportTemplate);
      exportOptions.setSearchParam(search);
      exportOptions.setMainService(this);
      exportOptions.addFieldMappingOption(
        "status",
        new CustomExcelExport.FieldMappingOption().setDictCode("SCM00150")
      );
      exportOptions.addFieldMappingOption(
        "logisticsProvider",
        new CustomExcelExport.FieldMappingOption()
          .setConverter((o, r) ->
            SalesOutboundLogisticsProviderEnums.toLabel((String) o)
          )
      );
      exportOptions.addFieldMappingOption(
        "currencyCode",
        new CustomExcelExport.FieldMappingOption().setDictCode("SCM00015")
      );
      exportOptions.addFieldMappingOption(
        "processingStatus",
        new CustomExcelExport.FieldMappingOption().setDictCode("SCM00027")
      );
      exportOptions.addFieldMappingOption(
        "fundTransferStatus",
        new CustomExcelExport.FieldMappingOption().setDictCode("SCM00026")
      );
      exportOptions.addFieldMappingOption(
        "dept",
        new CustomExcelExport.FieldMappingOption()
          .setConverter((o, r) ->
            String.valueOf(((SalesOutboundItemVo) r).getDeptName())
          )
      );
      exportOptions.addStringFields(
        "salesReleaseNumber",
        "systemMenuSymbol",
        "status",
        "secondHarmonicSignal",
        "platformNumber",
        "logisticsProvider",
        "transport",
        "tally",
        "settlementId",
        "fid",
        "amazonOrderId",
        "fulfillment",
        "countryCode",
        "accountType",
        "eventType",
        "sellerSku",
        "localSku",
        "localName",
        "type",
        "currencyCode",
        "processingStatus",
        "fundTransferStatus",
        "deptName",
        "sellerName"
      );

      String fileName = String.format(
        "销售出库单(%s).xlsx",
        DateUtils.toString(null, DateUtils.PATTERN_DATE_TIME)
      );
      ResponseUtils.setAttachmentFileName(response, fileName);
      customExcelExport.run(exportOptions, response.getOutputStream());
      return;
    }
    List<SysDictAllItem> statusItems = sysDictService.getDictItems("SCM00150");
    List<SysDictAllItem> currencyCodeItems = sysDictService.getDictItems(
      "SCM00015"
    );
    List<SysDictAllItem> processingStatusItems = sysDictService.getDictItems(
      "SCM00027"
    );
    List<SysDictAllItem> fundTransferStatusItems = sysDictService.getDictItems(
      "SCM00026"
    );
    search.setPageSize(EXPORT_QUERY_MAX_SIZE);
    int current = 1;
    List<SalesOutboundExcelPo> items = new ArrayList<SalesOutboundExcelPo>();
    while (true) {
      search.setCurrent(current);
      search.setSelectFields(Collections.singletonList("@export"));
      Page<SalesOutboundItemVo> page = this.pageSearch(search);
      if (page.getTotal() > EXPORT_MAX_SIZE) {
        throw new RException("导出记录数超出限制！");
      }
      List<SalesOutboundItemVo> list = page.getRecords();
      if (list.isEmpty()) {
        break;
      }
      for (SalesOutboundItemVo item : list) {
        SalesOutboundExcelPo excel = new SalesOutboundExcelPo();
        BeanUtils.copyProperties(item, excel);
        if (item.getStatus() != null) {
          statusItems
            .stream()
            .filter(i -> i.getItemValue().equals(item.getStatus()))
            .findFirst()
            .ifPresent(i -> excel.setStatus(i.getItemText()));
        }
        excel.setLogisticsProvider(
          SalesOutboundLogisticsProviderEnums.toLabel(
            item.getLogisticsProvider()
          )
        );
        if (item.getCurrencyCode() != null) {
          currencyCodeItems
            .stream()
            .filter(i -> i.getItemValue().equals(item.getCurrencyCode()))
            .findFirst()
            .ifPresent(i -> excel.setCurrencyCode(i.getItemText()));
        }
        if (item.getProcessingStatus() != null) {
          processingStatusItems
            .stream()
            .filter(i -> i.getItemValue().equals(item.getProcessingStatus()))
            .findFirst()
            .ifPresent(i -> excel.setProcessingStatus(i.getItemText()));
        }
        if (item.getFundTransferStatus() != null) {
          fundTransferStatusItems
            .stream()
            .filter(i -> i.getItemValue().equals(item.getFundTransferStatus()))
            .findFirst()
            .ifPresent(i -> excel.setFundTransferStatus(i.getItemText()));
        }

        items.add(excel);
      }
      if (list.size() < EXPORT_QUERY_MAX_SIZE) {
        break;
      }
      current++;
    }

    String fileName = String.format(
      "销售出库单(%s).xlsx",
      DateUtils.toString(null, DateUtils.PATTERN_DATE_TIME)
    );
    ResponseUtils.setAttachmentFileName(response, fileName);
    EasyExcel
      .write(response.getOutputStream())
      .registerWriteHandler(new CustomExcelHandler())
      .sheet()
      .head(SalesOutboundExcelPo.class)
      .doWrite(items);
  }

  /**
   * 通过订单号查询销售出库单主键(重复时返回最新的主键)
   */
  @Override
  public Long getIdByAmazonOrderId(String amazonOrderId) {
    return this.baseMapper.getIdByAmazonOrderId(amazonOrderId);
  }

  /**
   * 通过订单号查询销售出库单多主键(重复时返回最新的主键)
   */
  @Override
  public List<Long> getIdsByAmazonOrderId(List<String> list) {
    return this.baseMapper.getIdsByAmazonOrderId(list);
  }

  /**
   * 通过销售出库单主键查询订单号
   */
  @Override
  public String getAmazonOrderIdById(Long id) {
    return this.baseMapper.getAmazonOrderIdById(id);
  }

  /**
   * 通过销售出库单主键查询订单号列表
   */
  @Override
  public List<String> getAmazonOrderIdByIds(List<Long> ids) {
    List<Map<String, Object>> maps = this.baseMapper.getAmazonOrderIdByIds(ids);
    // 将返回重新排列为输入相同顺序
    return ids
      .stream()
      .map(id -> {
        Optional<Map<String, Object>> optionalMap = maps
          .stream()
          .filter(map -> id.equals(map.get("id")))
          .findFirst();
        return optionalMap
          .map(stringObjectMap -> (String) stringObjectMap.get("amazonOrderId"))
          .orElse(null);
      })
      .collect(Collectors.toList());
  }

  /**
   * 获取详情
   * @param id 销售出库单的主键
   */
  @DataActivity(action = SysDataActivityAction.View)
  @Override
  public SalesOutboundVo getVoById(Long id) throws Exception {
    SalesOutboundVo vo = this.baseMapper.selectVoById(id);
    if (vo == null) {
      return null;
    }
    return vo;
  }

  /**
   * 分页查询销售出库单
   * @param search 查询条件
   * @return 销售出库单分页查询结果
   */
  @Override
  public Page<SalesOutboundItemVo> pageSearch(
    SalesOutboundSearchParamPo search
  ) {
    Page<SalesOutboundItemVo> page = new Page<>();
    if (
      Objects.nonNull(search.getWfStatus()) &&
      StringUtils.hasText(search.getFormName())
    ) {
      List<WorkFlow> workFlowList = workFlowService.queryWfListByStatus(
        search.getWfStatus(),
        search.getFormName()
      );
      if (CollectionUtils.isEmpty(workFlowList)) {
        return page;
      }
      List<Long> ids = workFlowList
        .stream()
        .map(WorkFlow::getRecordId)
        .collect(Collectors.toList());
      if (CollectionUtils.isEmpty(ids)) {
        return page;
      }
      search.setSelectedIds(ids);
    }
    search.initSort("t.id desc", "t.", SORT_FIELD_MAP);
    page = this.baseMapper.pageSearch(search.toPage(), search);
    List<Long> recordIds = page
      .getRecords()
      .stream()
      .map(SalesOutboundItemVo::getId)
      .collect(Collectors.toList());
    if (CollectionUtils.isEmpty(recordIds)) {
      return page;
    }
    List<WorkFlow> workFlows = workFlowService.queryWfList(
      recordIds,
      Collections.singletonList(search.getFormName())
    );
    if (CollectionUtils.isEmpty(workFlows)) {
      return page;
    }
    Map<Long, Integer> flowMap = workFlows
      .stream()
      .collect(Collectors.toMap(WorkFlow::getRecordId, WorkFlow::getWfStatus));
    page
      .getRecords()
      .stream()
      .forEach(item -> {
        if (!flowMap.containsKey(item.getId())) {
          return;
        }
        item.setWfStatus(flowMap.get(item.getId()));
      });
    return page;
  }

  /**
   * 销售出库单快速查询选项(有缓存)
   * @param search 查询条件
   * @return 选项结果
   */
  @Override
  @Cacheable(
    value = CacheConfig.FAST,
    key = "'SalesOutboundOptions::' + @userInfo.userTypeAndId + '::' + #search.keyword"
  )
  public List<OptionItem<SalesOutboundItemVo>> searchOptions(
    SalesOutboundSearchParamPo search
  ) {
    search.initSort("t.id desc", "t.", SORT_FIELD_MAP);
    return this.baseMapper.searchOptions(search);
  }

  /**
   * 列表查询销售出库单
   * @param search 查询条件
   * @return 销售出库单列表查询结果
   */
  @Override
  public List<SalesOutboundItemVo> search(SalesOutboundSearchParamPo search) {
    search.initSort("t.id desc", "t.", SORT_FIELD_MAP);
    return this.baseMapper.search(search);
  }

  /**
   * 查询销售出库单最后更新时间
   * @param search 查询条件
   * @return 销售出库单最后更新时间
   */
  @Override
  public Date searchLastUpdateTime(SalesOutboundSearchParamPo search) {
    return this.baseMapper.searchLastUpdateTime(search);
  }

  /**
   * 全部推送
   */
  @DataActivity(
    action = SysDataActivityAction.Other,
    message = "%s执行了全部推送操作"
  )
  @Override
  public void shopPush() throws Exception {
    if (salesOutboundShopPushAction == null) {
      throw new Exception("此操作当前不可用");
    }
    salesOutboundShopPushAction.run();
  }
}
